using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;

using MySql.Data.MySqlClient;
using Oracle.DataAccess.Client;
using NUnit.Framework;

namespace runner
{
	/// <summary>
	/// Summary description for TestMigrationRunner.
	/// </summary>
	[TestFixture]
	public class TestMigrationRunner
	{
//		private static string connection_string = "Data Source=AEET;User Id=aee_sttl;Password=sttl_aee;";
//		private IDbConnection _connection = new OracleConnection(connection_string);
		private static string connection_string = "SERVER=localhost;DATABASE=migrations;UID=migration;PASSWORD=migration;";
		private IDbConnection _connection = new MySqlConnection(connection_string);
		private string _migrationpath = @"c:\usr\projects\migrations-net\src\runner\migrations";

		[SetUp]
		public void SetUp() 
		{
		}

		[TearDown]
		public void TearDown() 
		{
		}

		[Test]
		public void ExecuteToLargestVersion() 
		{
			MigrationRunner runner = new MigrationRunner(_connection, _migrationpath);
			int largest_version = FindLargetVersion(runner);
			Assert.AreEqual(largest_version, runner.Execute());
			foreach (string item in runner.MigrationChangeList.Print(ChangeListType.ALL, largest_version)) 
			{
				System.Console.Out.WriteLine(item);
			}
		}

		[Test]
		public void ExecuteToVersionOne() 
		{
			int initial_version = 1;
			MigrationRunner runner = new MigrationRunner(_connection, _migrationpath);
			Assert.AreEqual(initial_version, runner.Execute(initial_version));
			foreach (string item in runner.MigrationChangeList.Print(ChangeListType.ALL, initial_version)) 
			{
				System.Console.Out.WriteLine(item);
			}
		}

		[Test]
		public void ExecuteToVersionZero() 
		{
			int initial_version = 0;
			MigrationRunner runner = new MigrationRunner(_connection, _migrationpath);
			Assert.AreEqual(initial_version, runner.Execute(initial_version));
			foreach (string item in runner.MigrationChangeList.Print(ChangeListType.ALL, initial_version)) 
			{
				System.Console.Out.WriteLine(item);
			}
		}

		[Test]
		public void QueryVersion() 
		{
			MigrationRunner runner = new MigrationRunner(_connection, _migrationpath);
			Assert.Greater(runner.QueryVersion(), Constants.UNSPECIFIED_INTEGER);
		}

		private int FindLargetVersion(MigrationRunner runner) 
		{
			int largest_version = -1;

			IEnumerator iter = runner.Migrations.Keys.GetEnumerator();
			while(iter.MoveNext()) 
			{
				int version = (int)iter.Current;
				if (largest_version < version) 
				{
					largest_version = version;
				}
			}
			return largest_version;
		}

		private void Debug(object obj) 
		{
			System.Console.Out.WriteLine(obj.ToString());
		}
	}
}
